کلاس stack طراحی خودم
نوشته شده توسط :

قسمت main برنامه

public class Stack {
final int MAX_SIZE = 1024;
private int top;
private T[] buffer;

public Stack() {
top = 0;
//buffer = new T[MAX_SIZE];
buffer=(T[]) new Object[MAX_SIZE];
}

public boolean isEmpty() {
return (top == 0);
}

public boolean isFull() {
return (top == MAX_SIZE);
}

public void push(T x) throws FullStackException{
if (isFull()) {
throw new FullStackException("پشته پر است");
} else {
buffer[top] = x;
top++;
}
}

public T pop() throws EmptyStackException{
if (isEmpty()) {
throw new EmptyStackException("پشته خالی است");
}
top--;
return buffer[top];
}

public T top() {
return buffer[top - 1];
}

}

 


 

public class Stack_test {

public static void main(String[] args) {
// TODO Auto-generated method stub
Stack s = new Stack();
try {
s.pop();
s.push("mona");
s.push("hadavi");
s.push("20");
System.out.println(s.pop());
System.out.println(s.isFull());
System.out.println(s.isEmpty());
} catch (FullStackException fse) {
System.err.println(fse.getMessage());
} catch (EmptyStackException ese) {
System.err.println(ese.getMessage());
}
}

}


public class FullStackException extends Exception{
public FullStackException(String message){
super(message);
}
}


 

public class EmptyStackException extends Exception{
public EmptyStackException(String message){
super(message);
}
}


بازگشت به خانه

:: موضوعات مرتبط: برنامه سازی پیشرفته , ساختمان داده ها , ,
:: برچسب‌ها: کلاس , پشته ,
:: بازدید از این مطلب : 368
|
امتیاز مطلب : 0
|
تعداد امتیازدهندگان : 0
|
مجموع امتیاز : 0
تاریخ انتشار : دو شنبه 18 آبان 1394 | نظرات ()
مطالب مرتبط با این پست
لیست
می توانید دیدگاه خود را بنویسید


نام
آدرس ایمیل
وب سایت/بلاگ
:) :( ;) :D
;)) :X :? :P
:* =(( :O };-
:B /:) =DD :S
-) :-(( :-| :-))
نظر خصوصی

 کد را وارد نمایید:

آپلود عکس دلخواه: